package com.lipaluma.test.db.internal.assertions;

import static org.junit.Assert.assertEquals;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.joda.time.format.DateTimeFormat;

/**
 * test a datetime column
 * @author Mario
 */
class DateTimeColumn extends Column<Date> {

	private DateTimeColumn(Builder builder) {
		super(builder);
	}
	
	@Override
	public void assertValue(ResultSet rs) throws SQLException {
		if(name == null) {
			assertEquals(getErrorMessageOnIndexColumn(), expectedValue, rs.getTimestamp(index));
		} else {
			assertEquals(getErrorMessageOnNamedColumn(), expectedValue, rs.getTimestamp(name));
		}
	}

	public static DateTimeColumn.Builder builder(String request) {
		return new Builder(request);
	}

	public static class Builder extends Column.Builder<Date> {

		public Builder(String request) {
			super(request);
		}

		@Override
		public DateTimeColumn.Builder withExpectedValue(String expected) {
			this.expectedValue = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime(expected).toDate();
			return this;
		}

		@Override
		public DateTimeColumn build() {
			return new DateTimeColumn(this);
		}

	}
}
